package Demo;

import com.alibaba.druid.pool.DruidDataSource;
import connectionPool.ConnectionTest;
import org.junit.Test;

import java.io.IOException;
import java.sql.*;
import java.util.Properties;

/**
 * @author mucd
 * 2021/3/10/010 下午 04:19
 */

public class ConnectionMySql {
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    @Test
    public void getConnection(){
        Properties properties = new Properties();
        try {
            properties.load(ConnectionTest.class.getClassLoader().getResourceAsStream("2.properties"));
            DruidDataSource druidDataSource = new DruidDataSource();
            druidDataSource.configFromPropety(properties);
            connection = druidDataSource.getConnection();
            System.out.println(connection);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            System.out.println(e.toString());
        }catch (SQLException e){
            System.out.println(e.getMessage());
            System.out.println(e.toString());
            e.printStackTrace();
        }
    }

    public void closeAll(Connection connection, Statement statement,ResultSet resultSet){
        if (connection != null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (preparedStatement != null){
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (resultSet != null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
